package com.wyp168.leetcode;

public class LongestCommonPrefix {

    public static void main(String[] args) {

        System.out.println(longestCommonPrefix(new String[]{"c", "acc", "ccc"}));


    }


    public static String longestCommonPrefix(String[] strs) {
        //边界值判断
        if (strs == null || strs.length == 0) {
            return "";
        }
        //假定字符串数组中的第一个字符串为标准字串
        String str = strs[0];
        //定义变量，表示字符串格式
        int slength = strs.length;
        while(str != null || str.equals("")) {
            boolean flag = true;
            for (int i = 1; i < strs.length; i ++) {
                if (strs[i].indexOf(str) == 0) {
                    continue;
                } else {
                    flag = false;
                    break;
                }
            }
            if (flag == false) {
                str = str.substring(0,str.length()-1);
            } else {
                break;
            }
        }
        return str;
    }
}
